package com.fruit.service.goodspackage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.goodspackage.detail.GoodsPackageDetailMapper;
import com.fruit.service.goodspackage.GoodsPackageDetailManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 产品套餐权益详情相关服务
 * 
 * @author Administrator
 * @version 2018.02.02 11:40
 */
@Slf4j
@Service("goodsPackageDetailService")
public class GoodsPackageDetailService implements GoodsPackageDetailManager {
	@Autowired
	private GoodsPackageDetailMapper goodsPackageDetailMapper;

	@Override
	public Map<String, Object> getRecords(Map<String, Object> params) {
		log.info("【产品套餐权益列表服务】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (params == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
			return result;
		}
		log.info("【产品套餐权益列表服务】---【params】---{}", params.toString());
		if (params.get("goodsPackageId") == null || "".equals(params.get("goodsPackageId"))) { // 套餐id
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入套餐id不能为空!"));
			return result;
		}
		try {
			// 查询产品套餐列表
			Long goodsPackageId = Long.parseLong(""+params.get("goodsPackageId"));
			List<Map<String, Object>> list = goodsPackageDetailMapper.getRecordsByTime(goodsPackageId);
			log.info("【产品套餐权益列表服务】---【批次时间获取成功】---");
			// 时间批次
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> pam = new HashMap<String, Object>();
				pam.put("goodsPackageId", goodsPackageId);
				pam.put("startTime", list.get(i).get("startTime"));
				pam.put("endTime",list.get(i).get("endTime"));
				List<Map<String, Object>> detailList = goodsPackageDetailMapper.getRecords(pam);
				if (detailList == null || detailList.size() <= 0) {
					log.error("【产品套餐权益列表服务】---【detailList异常】---{}", list.get(i).toString());
					result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "获取发货时间范围内商品异常"));
					return result;
				}
				// 放入每批次的商品列表
				list.get(i).put("detailList", detailList);
			}
			log.info("【产品套餐权益列表服务】---【批次时间内商品列表获取成功】---");
			result.put("data", list);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【产品套餐权益列表服务】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【产品套餐权益列表服务】---【结束】---");
		return result;
	}

	@Override
	public Map<String, Object> selectByPackageId(Map<String, Object> params) {
		log.info("【产品套餐详情服务】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (params == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
			return result;
		}
		log.info("【产品套餐详情服务】---【params】---{}", params.toString());
		if (params.get("goodsPackageId") == null || "".equals(params.get("goodsPackageId"))) { // 套餐id
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入套餐id不能为空!"));
			return result;
		}
		try {
			// 查询产品套餐列表
			Long goodsPackageId = Long.parseLong(""+params.get("goodsPackageId"));
			List<Map<String, Object>> list = goodsPackageDetailMapper.selectByPackageId(goodsPackageId);
			result.put("data", list);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【产品套餐详情服务】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【产品套餐详情服务】---【结束】---");
		return result;
	}

}
